﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PCMSofCC;

namespace PCMSofCC.BLL
{
    public static class Sites
    {
        public static IQueryable<Site> GetAssignedSites(Guid _userId, DateTime _date)
        {
            PCMSofCCDataContext db = new PCMSofCCDataContext();
            var q =
                from s in db.Record
                where s.UserId == _userId && s.Date == _date
                select s.Site;
            return q;
        }

        public static IQueryable<Site> GetAssignedSites(DateTime _date)
        {
            PCMSofCCDataContext db = new PCMSofCCDataContext();
            var q =
                from s in db.Record
                where s.Date == _date
                select s.Site;
            return q;
        }

        public static IQueryable<Site> GetUnassignedSitesOnSomeDay(DateTime _date)
        {
            PCMSofCCDataContext db = new PCMSofCCDataContext();
            var q =
                from s in db.Site
                where !s.Record.Any(r => r.Date == _date)
                select s;
            return q;
        }

        public static void AddSite(string _name, string _number, string _address, double _lon, double _lati, int _companyId)
        {
            PCMSofCCDataContext db = new PCMSofCCDataContext();
            var newSite = new Site
            {
                Name = _name,
                Number = _number,
                Address = _address,
                Longitude = _lon,
                Latitude = _lati,
                CompanyId = _companyId,
                IsUsed = 1
            };
            db.Site.InsertOnSubmit(newSite);
            db.SubmitChanges();
        }

        public static Site GetSite(int _siteId)
        {
            PCMSofCCDataContext db = new PCMSofCCDataContext();
            var query =
                from s in db.Site
                where s.Id == _siteId
                select s;
            return query.Single();
        }

        public static void UpdateSite(int _siteId, Func<Site, Site> predict)
        {
            PCMSofCCDataContext db = new PCMSofCCDataContext();
            Site site = db.Site.Single(x => x.Id == _siteId);
            site = predict(site);
            db.SubmitChanges();
        }
    }
}
